package com.yummy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yummy.dto.QueryEmployeeVo;
import com.yummy.entity.Employee;
import com.yummy.mapper.EmployeeMapper;
import com.yummy.service.EmployeeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author yinbo
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {

	Logger logger = LoggerFactory.getLogger(EmployeeServiceImpl.class);

	@Autowired
	EmployeeMapper empMapper;

	@Override
	public Employee login(String username, String password){
		return empMapper.selectByNameAndPassword(username,password);
	}

	@Override
	public Employee findEmpByName(String username) {
		return empMapper.selectByName(username);
	}

	@Override
	public boolean registerAdmin(Employee employee) {
		return empMapper.insertEmployee(employee)>0;
	}

	@Override
	public PageInfo<Employee> queryByPageAndCondition(QueryEmployeeVo queryEmployeeVo) {
		Page<Employee> page = PageHelper.startPage(queryEmployeeVo.getCurrentPage(), queryEmployeeVo.getPageSize());
		logger.info(queryEmployeeVo.toString());
		List<Employee> employees = empMapper.selectAllByCondition(queryEmployeeVo);
		PageInfo<Employee> pageInfo = page.toPageInfo();

		return pageInfo;
	}

	@Override
	public boolean updateEmployee(Employee employee) {
		return empMapper.updateByPrimaryKey(employee)>0;
	}

	@Override
	public Employee findEmpById(String userId) {
		Employee employee = empMapper.selectByPrimaryKey(userId);
		return employee;
	}

	@Override
	public boolean addEmployee(Employee employee) {
		return empMapper.insertEmployee(employee)>0;
	}

	@Override
	public boolean editEmployee(Employee employee) {
		return empMapper.updateEmployee(employee)>0;
	}

	@Override
	public boolean removeEmployees(String... ids) {
		return empMapper.deleteByPrimaryKeys(ids)>0;
	}

	@Override
	public List<Employee> queryAll() {
		return empMapper.selectAll();
	}

	@Override
	public boolean insertBatch(List<Employee> employees) {
		return empMapper.insertEmployees(employees)>0;
	}

	@Override
	public boolean updateStatus(String id, String status) {

		return empMapper.updateStatusById(id,status)>0;
	}
}
